function login() {
	var url = "/personWeb/user/login"
	var name = $("#user-name").val()
	var psw = $("#user-psw").val();
	if ($("#user-name").val() == "") {
		alert("用户名不能为空");
		return false;
	} else if ($("#user-psw").val() == "") {
		alert("密码不能为空");
		return false;
	} else {
		$.ajax({
			url : url,
			type : 'POST',
			dataType : 'json',
			contentType : 'text/html',
			data : JSON.stringify({
				userName : $("#user-name").val(),
				userPassword : $("#user-psw").val()
			}),
			success : function(data, status) {
				var status = status;
				if (status.toString() == "success") {
					top.window.location = "../index.html";
				} else {
					window.location.href = "login.html";
				}
			},
			error : function() {
				alert("用户名密码验证失败")
			}
		});
		return false;
	}
};


function register(){
	var imgSession = getImgCode()
	var url = "/personWeb/user/register"
	var reUserName = $("#username").val()
	var reUserPw = $("#password").val()
	var reConfirmpassword = $("#confirmpassword").val()
	var number = $("#code").val();
	if (reUserName == "") {
	    alert("用户名不能为空");
	}else if (reUserPw == "") {
	    alert("密码不能为空");
	}else if (reConfirmpassword == "") {
	    alert("密码不能为空");
	}else if (reUserPw != reConfirmpassword){
		alert("两次密码不一致");
	}else if(number==""){
		alert("请输入验证码");
	}else if(number!=imgSession){
		alert("验证码错误，请重新输入");
    }else {
    	$.ajax({
    		url: url,
    		async: true,
            type: 'POST',
            dataType : 'json',
            contentType : 'text/html',
            data:JSON.stringify({
            	userName: reUserName,
            	userPassword: reUserPw
            }),
            success: function (data, status) {
                var status = status;
                if (status.toString() == "success") {
                	alert('注册成功，请登录');
                	top.window.location ="login.html";
                }
                else{
                	window.location.href="";
                }
            },
            error: function () { alert("用户名密码验证失败") }
    	});
    }
};

function getImgCode(){
	var imgSession =""
	var url = "/personWeb/user/getsession";
	$.ajax({
		async: false,
		url: url,
        type: 'GET',
        dataType : 'json',
        success: function (data, status) {
            var status = status
            if (status.toString() == "success") {
            	imgSession=data.imgCode
            }
        },
        error: function () { alert("获取imgCode失败") }
	});
	return imgSession;
};

/*function getUserName(){
	var userName =""
	var url = "/personWeb/user/getsession";
	$.ajax({
		async: false,
		url: url,
        type: 'GET',
        dataType : 'json',
        success: function (data, status) {
            var status = status
            if (status.toString() == "success") {
            	userName=data.userName
            }
        },
        error: function () { alert("获取name失败") }
	});
	return userName;
}*/
